package biz

import (
	"context"
	"time"

	"lianzi_gos/internal/mods/mpweixin/dal"
	"lianzi_gos/internal/mods/mpweixin/schema"
	"lianzi_gos/pkg/errors"
	"lianzi_gos/pkg/util"
)

// 我要买
type Procure struct {
	Trans      *util.Trans
	ProcureDAL *dal.Procure
}

// Query procures from the data access object based on the provided parameters and options.
func (a *Procure) Query(ctx context.Context, params schema.ProcureQueryParam) (*schema.ProcureQueryAdminResult, error) {
	params.Pagination = false
	params.WID = util.FromUserID(ctx)
	result, err := a.ProcureDAL.Query(ctx, params, schema.ProcureQueryOptions{
		QueryOptions: util.QueryOptions{
			OrderFields: []util.OrderByParam{
				{Field: "sequence", Direction: util.DESC},
				{Field: "types", Direction: util.DESC},
				{Field: "created_at", Direction: util.DESC},
			},
		},
	})
	if err != nil {
		return nil, err
	}
	return result, nil
}

// Query procures from the data access object based on the provided parameters and options.
func (a *Procure) QueryAdmin(ctx context.Context, params schema.ProcureQueryParam) (*schema.ProcureQueryAdminResult, error) {
	params.Pagination = false

	result, err := a.ProcureDAL.QueryAdmin(ctx, params, schema.ProcureQueryOptions{
		QueryOptions: util.QueryOptions{
			OrderFields: []util.OrderByParam{
				{Field: "sequence", Direction: util.DESC},
				{Field: "types", Direction: util.DESC},
				{Field: "created_at", Direction: util.DESC},
			},
		},
	})
	if err != nil {
		return nil, err
	}
	return result, nil
}

// Query procures from the data access object based on the provided parameters and options.
func (a *Procure) UserQuery(ctx context.Context, params schema.ProcureQueryParam) (*schema.ProcureQueryAdminResult, error) {
	params.Pagination = true
	params.WID = util.FromUserID(ctx)
	result, err := a.ProcureDAL.Query(ctx, params, schema.ProcureQueryOptions{
		QueryOptions: util.QueryOptions{
			OrderFields: []util.OrderByParam{
				{Field: "sequence", Direction: util.DESC},
				{Field: "types", Direction: util.DESC},
				{Field: "created_at", Direction: util.DESC},
			},
		},
	})
	if err != nil {
		return nil, err
	}
	return result, nil
}

// Get the specified procure from the data access object.
func (a *Procure) Get(ctx context.Context, id string) (*schema.Procure, error) {
	procure, err := a.ProcureDAL.Get(ctx, id)
	if err != nil {
		return nil, err
	} else if procure == nil {
		return nil, errors.NotFound("", "Procure not found")
	}
	return procure, nil
}

// Create a new procure in the data access object.
func (a *Procure) Create(ctx context.Context, formItem *schema.ProcureForm) (*schema.Procure, error) {
	procure := &schema.Procure{
		ID:        util.NewXID(),
		CreatedAt: time.Now(),
	}

	if err := formItem.FillTo(procure); err != nil {
		return nil, err
	}
	procure.UID = util.FromUserID(ctx)
	err := a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.ProcureDAL.Create(ctx, procure); err != nil {
			return err
		}
		return nil
	})
	if err != nil {
		return nil, err
	}
	return procure, nil
}

// Update the specified procure in the data access object.
func (a *Procure) Update(ctx context.Context, id string, formItem *schema.ProcureForm) error {
	procure, err := a.ProcureDAL.Get(ctx, id)
	if err != nil {
		return err
	} else if procure == nil {
		return errors.NotFound("", "Procure not found")
	}

	if err := formItem.FillTo(procure); err != nil {
		return err
	}
	procure.UpdatedAt = time.Now()

	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.ProcureDAL.Update(ctx, procure); err != nil {
			return err
		}
		return nil
	})
} // Update the specified procure in the data access object.
func (a *Procure) UpdateContact(ctx context.Context, id string, formItem *schema.ProcureContactForm) error {
	procure, err := a.ProcureDAL.Get(ctx, id)

	if err != nil {
		return err
	} else if procure == nil {
		return errors.NotFound("", "Procure not found")
	}
	if err := formItem.FillToContact(procure); err != nil {
		return err
	}
	procure.UpdatedAt = time.Now()
	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.ProcureDAL.UpdateContact(ctx, procure); err != nil {
			return err
		}
		return nil
	})
}
func (a *Procure) UpdateContactType(ctx context.Context, id string) error {
	procure, err := a.ProcureDAL.Get(ctx, id)

	if err != nil {
		return err
	} else if procure == nil {
		return errors.NotFound("", "Procure not found")
	}
	procure.OkAt = time.Now()
	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.ProcureDAL.UpdateContactType(ctx, procure); err != nil {
			return err
		}
		return nil
	})
}

// Delete the specified procure from the data access object.
func (a *Procure) Delete(ctx context.Context, id string) error {
	exists, err := a.ProcureDAL.Exists(ctx, id)
	if err != nil {
		return err
	} else if !exists {
		return errors.NotFound("", "Procure not found")
	}

	return a.Trans.Exec(ctx, func(ctx context.Context) error {
		if err := a.ProcureDAL.Delete(ctx, id); err != nil {
			return err
		}
		return nil
	})
}
